<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="发起人" prop="nickName">
        <el-input
          v-model="queryParams.nickName"
          placeholder="请输入邀请人名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="开始日期" prop="startDate">
        <el-date-picker clearable
                        v-model="queryParams.startDate"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="开始日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="结束日期" prop="endDate">
        <el-date-picker clearable
                        v-model="queryParams.endDate"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="结束日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-check"
          size="mini"
          :disabled="multiple"
          @click="handleAgree"
          v-hasPermi="['meeting:manage:agree']"
        >同意
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-close"
          size="mini"
          :disabled="multiple"
          @click="handleRefused"
          v-hasPermi="['meeting:manage:refused']"
        >拒绝
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['meeting:manage:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="manageList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="发起人" align="center" prop="nickName"/>
      <el-table-column label="性别" align="center" prop="sex">
        <template slot-scope="scope">
          <span>{{ scope.row.sex === '0' ? "男" : scope.row.sex === '1' ? "女" : "未知" }} </span>
        </template>
      </el-table-column>
      <el-table-column label="手机号" align="center" prop="phoneNumber"/>
      <el-table-column label="会议开始时间" align="center" prop="startTime">
        <template slot-scope="scope">
          <span>{{  parseTime(scope.row.startTime,'{y}-{m}-{d} {h}:{i}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="会议名称" align="center" prop="meetingName"/>
      <el-table-column label="发起时间" align="center" prop="meetingLaunchDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.meetingLaunchDate,'{y}-{m}-{d} {h}:{i}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="会议密码" align="center" prop="password"/>
      <el-table-column label="状态" align="center" prop="state">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.meeting_invite_state" :value="scope.row.state"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-check"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['meeting:manage:agree']"
          >同意
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-close"
            @click="handleRefused(scope.row)"
            v-hasPermi="['meeting:manage:refused']"
          >拒绝
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改我的会议管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="拒绝理由" prop="reason">
          <el-input v-model="form.reason" placeholder="请输拒绝理由"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {listManage,refusedManage, agree} from "@/api/meeting/manage"
import {parseTime} from "../../../utils/ruoyi";

export default {
  name: "Manage",
  dicts: ['meeting_invite_state'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 我的会议管理表格数据
      manageList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        startDate: null,
        endDate: null,
        nickName: null,
      },
      // 表单参数
      form: {},
      reasonIds: '',
      // 表单校验
      rules: {
        reason: [
          {required: true, message: "理由不能为空", trigger: "blur"}
        ],
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    parseTime,
    /** 查询我的会议管理列表 */
    getList() {
      this.loading = true
      listManage(this.queryParams).then(response => {
        this.manageList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        reason: null
      }
      this.resetForm("form")
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm")
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 同意按钮操作 */
    handleAgree() {
      this.$confirm('你真的要同意吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        let meetingids = []
        this.ids.map(item => meetingids.push(item))
        agree(meetingids).then((res) => {
          this.$message({
            type: 'success',
            message: `同意${res.data}个会议!`
          });
          // 从新查询一边更新页面
          this.getList()
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消同意'
        });
      });
    },
    /** 同意单选按钮操作 */
    handleUpdate(row) {
      const id = row.id || this.ids
      this.$confirm('你真的要同意吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        agree(id).then((res) => {
          this.$message({
            type: 'success',
            message: `同意${res.data}个会议!`
          });
          // 从新查询一边更新页面
          this.getList()
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消同意'
        });
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.title = '拒绝会议'
          refusedManage(this.reasonIds,this.form).then((response) => {
            this.$modal.msgSuccess(`已拒绝${response.data}个会议`)
          })
          this.open = false
          this.getList()
          this.form = {}
        }
      })
    },
    /** 拒绝会议按钮操作 */
    handleRefused(row) {
      this.reasonIds = row.id || this.ids
      this.open = true;
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('meeting/manage/export', {
        ...this.queryParams
      }, `manage_${new Date().getTime()}.xlsx`)
    }
  }
}
</script>
